package com.wkg.springblogdemo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wkg.springblogdemo.common.exception.BlogException;
import com.wkg.springblogdemo.common.util.BeanTransformUtils;
import com.wkg.springblogdemo.common.util.JwtUtils;
import com.wkg.springblogdemo.common.util.Md5Util;
import com.wkg.springblogdemo.constant.Constants;
import com.wkg.springblogdemo.mapper.BlogMapper;
import com.wkg.springblogdemo.mapper.UserMapper;
import com.wkg.springblogdemo.pojo.dataobject.BlogInfo;
import com.wkg.springblogdemo.pojo.dataobject.UserInfo;
import com.wkg.springblogdemo.pojo.request.UserLoginRequest;
import com.wkg.springblogdemo.pojo.response.UserInfoResponse;
import com.wkg.springblogdemo.pojo.response.UserLoginResponse;
import com.wkg.springblogdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public UserLoginResponse login(UserLoginRequest userLoginRequest) {
        UserInfo userInfo = queryUserByName(userLoginRequest.getUserName());
        log.info("已获取到登录信息");
        if (userInfo == null || userInfo.getId() <= 0) {
            throw new BlogException(201,"输入用户不存在,请重写输入");
        }

        //判断密码是否正确
        /*if (!userLoginRequest.getPassword().equals(userInfo.getPassword())) {
            throw new BlogException("密码不正确");
        }*/
        if (!Md5Util.verify(userLoginRequest.getPassword(), userInfo.getPassword())) {
            log.warn("登录失败: 密码错误 - 用户ID:{}", userInfo.getId());
            throw new BlogException(202,"输入的密码错误,请重写输入");
        }


        Map<String, Object> claims = new HashMap<>();
        claims.put("id", userInfo.getId());
        claims.put("userName", userInfo.getUserName());

        return new UserLoginResponse(userInfo.getId(), JwtUtils.getToken(claims));
    }


    public UserInfo queryUserByName(String name) {
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserInfo::getUserName, name).eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE);

        return userMapper.selectOne(lambdaQueryWrapper);
    }


    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        //代码一
        UserInfo userInfo = getUserInfoById(userId);
        if (userInfo == null || userInfo.getId() <= 0) {
            throw new BlogException("userInfo不合法");
        }

        return BeanTransformUtils.trans(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = getBlogInfoById(blogId);
        if (blogInfo == null || blogInfo.getUserId() <= 0) {
            throw new BlogException("blogInfo不合法");
        }
        UserInfo userInfo = getUserInfoById(blogInfo.getUserId());
        if (userInfo == null || userInfo.getId() <= 0) {
            throw new BlogException("userInfo不合法");
        }

        return BeanTransformUtils.trans(userInfo);
    }

    public UserInfo getUserInfoById(Integer userId) {
        return userMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId, userId)
                .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE));
    }


    public BlogInfo getBlogInfoById(Integer blogId) {
        return blogMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE));
    }


}
